package com.koicarp.university.graduate.service.service.schoolInfoManage.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graduate.service.dao.AcademyDao;
import com.koicarp.university.graduate.service.entity.schoolInfoManage.Academy;
import com.koicarp.university.graduate.service.service.schoolInfoManage.IAcademyService;
import com.koicarp.university.graduate.service.vo.schoolInfoManage.AcademyVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.utils.UniversityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 【学校基本信息：学院管理表】(Academy)表服务实现类
 *
 * @author liutao
 * @since 2020-07-30 17:23:16
 */
@Slf4j
@Service("academyService")
@RequiredArgsConstructor
public class AcademyServiceImpl extends ServiceImpl<AcademyDao, Academy> implements IAcademyService {

    private final AcademyDao academyDao;

    public UniversityResponse getAllAcademyInfo(Academy academy){
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        List<Academy> list = this.academyDao.queryAll(academy);
//        Map<String, Object> dataTable = UniversityUtil.getDataTable(list);
        response.data(list);
        return response;
    }
    @Override
    public UniversityResponse getAcademyInfoWithPage(AcademyVo vo) {
        UniversityResponse response = new UniversityResponse();
        Page<Academy> page = null;
        boolean findOne = UniversityUtil.isNotNull(vo.getId());
        boolean isPage = UniversityUtil.isNotNull(vo.getPageNum())
                && UniversityUtil.isNotNull(vo.getPageSize());
        Academy academy=new Academy();
        BeanUtils.copyProperties(vo,academy);
        if (!findOne && !isPage){
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map listRes = new HashMap();
            listRes.put(BaseConstant.RESPONSE_RESULTSET,this.academyDao.queryAll(academy));
            response.data(listRes);
            //response.data(this.tParkInfoDao.selectTParkInfoDto(tParkInfoDto));
            return response;
        }
        if (findOne){
            page = new Page<>(1,1,false);
        } else{
            page = new Page<>(vo.getPageNum(),vo.getPageSize(),true);
        }
        IPage<Academy> result = null;
        if(UniversityUtil.isNotNull(vo.getAcademyName())){
            result = this.academyDao.queryBlurAcademyInfoWithPage(page,vo);
        }else {
            result = this.academyDao.queryAcademyInfoWithPage(page, vo);
        }
        if (findOne){
        List<Academy> list = result.getRecords();
        if (list == null || list.size() == 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_NULL);
        } else {
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            response.data(list.get(0));
        }
        } else {
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map<String, Object> dataTable = UniversityUtil.getDataTable(result);
            response.data(dataTable);
        }
        return response;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Academy queryById(Integer id) {
        return this.academyDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Academy> queryAllByLimit(int offset, int limit) {
        return this.academyDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param academy 实例对象
     * @return 实例对象
     */
    @Override
    public UniversityResponse insert(Academy academy) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        LambdaQueryWrapper<Academy> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Academy::getAcademyCode,academy.getAcademyCode()).or().eq(Academy::getAcademyName,academy.getAcademyName());
        List<Academy> academies = academyDao.selectList(wrapper);
        if (!academies.isEmpty()){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message("学院代码或学院名称"+BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_DUPLICATION);
            return response;
        }
        int i = this.academyDao.insert(academy);
        if (i<0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
            return response;
        }
        response.data(i);
        return response;
    }

    /**
     * 修改数据
     *
     * @param academy 实例对象
     * @return 实例对象
     */
    @Override
    public UniversityResponse update(Academy academy) {
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        LambdaQueryWrapper<Academy> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Academy::getAcademyName,academy.getAcademyName()).or().eq(Academy::getAcademyCode,academy.getAcademyCode());
        List<Academy> academies = academyDao.selectList(wrapper);
        if (!academies.isEmpty()){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message("学院代码或学院名称"+BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_DUPLICATION);
            return response;
        }
        int i = this.academyDao.update(academy);
        if (i < 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public UniversityResponse deleteById(Integer id) {
        int i = this.academyDao.deleteById(id);
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        if (i > 0){
            return response;
        }
        response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        return response;
    }

    @Override
    public UniversityResponse deleteList(List<Integer> list) {
        int i =this.academyDao.deleteListById(list);
        UniversityResponse response =new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        if (i<0){
          response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
          response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
}